package org.jjche.core.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.validation.Validator;

/**
 * <p>
 * 国际化
 * </p>
 *
 * @author miaoyj
 * @since 2024-06-27
 */
@Configuration
@AutoConfigureBefore({WebMvcAutoConfiguration.class, WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
public class LocaleMvcConfiguration implements WebMvcConfigurer {

//    @Bean //冲突
//    public LocaleResolver localeResolver() {
//        AcceptHeaderLocaleResolver slr = new AcceptHeaderLocaleResolver();
//        slr.setDefaultLocale(Locale.US);
//        return slr;
//    }

    /**
     * 配置JSR-303国际化
     *
     * @param messageSource 消息源
     * @return
     */
    @Bean
//    @Primary
    public Validator getValidatorJSR(@Autowired MessageSource messageSource) {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource);
        return validator;
    }
}
